package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.inter.ISelectBgClick;

/**
 * Created by Administrator on 2018/7/11.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class MainSkinAdapter extends RecyclerView.Adapter<MainSkinAdapter.MyViewHodler> {
    private Integer[] updateBg = new Integer[]{R.drawable.update_bg_selector_01, R.drawable.update_bg_selector_02,
            R.drawable.update_bg_selector_03, R.drawable.update_bg_selector_04, R.drawable.update_bg_selector_05, R.drawable.update_bg_selector_06};

    private Context context;
    private ISelectBgClick mISelectBgClick = null;

    public void setmISelectBgClick(ISelectBgClick mISelectBgClick) {
        this.mISelectBgClick = mISelectBgClick;
    }

    public MainSkinAdapter(Context mContext) {
        context = mContext;
    }

    @Override
    public MainSkinAdapter.MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_skin, parent, false);
        MyViewHodler myViewHodler = new MyViewHodler(view);
        return myViewHodler;
    }

    @Override
    public void onBindViewHolder(final MainSkinAdapter.MyViewHodler holder, final int position) {
        holder.iv_update_bg.setBackgroundResource(updateBg[position]);
        final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        int selector = mSharedPreferences.getInt("item_skin",0); //todo 名称
        // todo 点击哪个，显示哪个

        if(selector == position){
            holder.rl_selector.setVisibility(View.VISIBLE);
        }else{
            holder.rl_selector.setVisibility(View.GONE);
        }
       // Log.e("MainSkinAdapter", "onClick selector : "+selector );
        holder.iv_update_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mISelectBgClick != null){
                    mISelectBgClick.onItemClicke(position);
                }
            }
        });


    }
    @Override
    public int getItemCount() {
        return updateBg.length;
    }

    class MyViewHodler extends RecyclerView.ViewHolder {

        private final ImageView iv_update_bg;
        private final RelativeLayout rl_selector;
        private final RelativeLayout rl_focuse;

        public MyViewHodler(View itemView) {
            super(itemView);
            iv_update_bg = itemView.findViewById(R.id.item_skin_iv_update_bg);
            rl_selector = itemView.findViewById(R.id.item_skin_rl_selector);
            rl_focuse = itemView.findViewById(R.id.item_skin_rl_focuse);
        }
    }
}
